package com.shop.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.entity.dto.WeChatAddress.UserAddressGetUpdateDto;
import com.shop.entity.dto.WeChatAddress.UserAddressRemoveDto;
import com.shop.entity.dto.WeChatAddress.UserAddressSaveDto;
import com.shop.entity.dto.WeChatAddress.UserAddressUpdateDto;
import com.shop.entity.pojo.Address;
import com.shop.entity.pojo.User;
import com.shop.user.mapper.AddressMapper;
import com.shop.user.mapper.UserMapper;
import com.shop.user.service.AddressService;
import com.shop.utils.common.enums.Code;
import com.shop.utils.common.enums.Message;
import com.shop.utils.common.result.Result;
import com.shop.utils.config.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address>
    implements AddressService{

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private AddressMapper addressMapper;

    @Override
    public Result getUserAddressList() {
        Long userId = BaseContext.getCurrentId();
        User user = userMapper.selectById(userId);
        if (user==null||userId==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        String k= Message.WeChat_Key_User_Address+user.getId();
        List<Address> check = (List<Address>) redisTemplate.opsForValue().get(k);
        if (check!=null&&check.size()> Message.Check_Int_Null) {
            return Result.okResult(check);
        }
        List<Address> v = list(Wrappers.<Address>lambdaQuery().eq(Address::getUserId, user.getId()));
        redisTemplate.opsForValue().set(k,v,1, TimeUnit.HOURS);
        return Result.okResult(v);
    }

    @Override
    public Result saveUserAddress(UserAddressSaveDto dto) {
        if (dto==null) {
            return Result.errorResult(Code.PARAM_REQUIRE);
        }
        Long userId = BaseContext.getCurrentId();
        User user = userMapper.selectById(userId);
        if (userId==null||user==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        String k=Message.WeChat_Key_User_Address+userId;
        redisTemplate.delete(k);
        Address address=new Address();
        address= address.builder()
                .ordersAddress(dto.getAddress())
                .ordersAddressDetail(dto.getAddressDetail())
                .ordersPhone(dto.getPhone())
                .userId(userId).build();
        int check = addressMapper.insert(address);
        if (check== Message.Check_Int_False) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        redisTemplate.delete(k);
        return Result.okResult(Code.SUCCESS);
    }

    @Override
    public Result removeUserAddress(UserAddressRemoveDto dto) {
        Long userId = BaseContext.getCurrentId();
        User user = userMapper.selectById(userId);
        if (user==null||userId==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        String k=Message.WeChat_Key_User_Address+userId;
        redisTemplate.delete(k);
        int check = addressMapper.deleteById(dto.getAddressId());
        if (check== Message.Check_Int_False) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        redisTemplate.delete(k);
        return Result.okResult(Code.SUCCESS);
    }

    @Override
    public Result getUpdateUserAddress(UserAddressGetUpdateDto dto) {
        if (dto==null) {
            return Result.errorResult(Code.PARAM_REQUIRE);
        }
        Long userId = BaseContext.getCurrentId();
        User user = userMapper.selectById(userId);
        if (userId==null||user==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        Address check = addressMapper.selectOne(Wrappers.<Address>lambdaQuery()
                .eq(Address::getId,dto.getAddressId())
                .eq(Address::getUserId,userId));
        if (check==null) {
            return Result.errorResult(Code.DATA_NOT_EXIST);
        }
        return Result.okResult(check);
    }

    @Override
    public Result updateUserAddress(UserAddressUpdateDto dto) {
        if (dto==null) {
            return Result.errorResult(Code.PARAM_REQUIRE);
        }
        Long userId = BaseContext.getCurrentId();
        User user = userMapper.selectById(userId);
        if (userId==null||user==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        String k=Message.WeChat_Key_User_Address+userId;
        redisTemplate.delete(k);
        Address old = addressMapper.selectOne(Wrappers.<Address>lambdaQuery()
                .eq(Address::getId, dto.getAddressId())
                .eq(Address::getUserId, userId));
        Address address=new Address();
        address.setId(dto.getAddressId());
        address.setOrdersAddress(old.getOrdersAddress());
        address.setOrdersAddressDetail(old.getOrdersAddressDetail());
        address.setOrdersPhone(old.getOrdersPhone());
        if (dto.getPhone()!=null) {
            address.setOrdersPhone(dto.getPhone());
        }
        if (dto.getAddress()!=null) {
            address.setOrdersAddress(dto.getAddress());
        }
        if (dto.getAddressDetail()!=null) {
            address.setOrdersAddressDetail(dto.getAddressDetail());
        }
        int check = addressMapper.updateById(address);
        if (check== Message.Check_Int_False) {
            return Result.errorResult(Code.SERVER_ERROR);
        }

        return Result.okResult(Code.SUCCESS);
    }
}




